﻿using System;
using Monkey.Framework.Logging;
using Monkey.Warrenty.Data;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;

namespace Monkey.Warrenty.Biz
{
    public class DealerGroupBiz
    {
        #region Properties

        private readonly IDealerGroupDao _dealerGroupDao;

        #endregion

        #region Constructors

        public DealerGroupBiz()
        {
            _dealerGroupDao = new DealerGroupDao();
        }

        public DealerGroupBiz(IDealerGroupDao dealerGroupDao)
        {
            _dealerGroupDao = dealerGroupDao;
        }

        #endregion

        #region Methods

        public DealerGroup GetById(int id)
        {
            if (id <= 0) return null;
            try
            {
                return _dealerGroupDao.GetById(id);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }

            return null;
        }

        public DealerGroup CreateGroup(DealerGroup dealerGroup)
        {
            // validate inputs
            if (dealerGroup == null) return null;
            if (string.IsNullOrEmpty(dealerGroup.Code) || string.IsNullOrEmpty(dealerGroup.Name)) return null;

            try
            {
                return _dealerGroupDao.AddDealerGroup(dealerGroup);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
                return null;
            }
        }

        public int GetMaxId()
        {
            return _dealerGroupDao.GetMaxId();
        }

        public int GetNextCode()
        {
            return _dealerGroupDao.GetCurrentIdentity() + 1;
        }

        public void UpdateGroup(DealerGroup dealerGroup)
        {
            if (dealerGroup == null) return;
            try
            {
                _dealerGroupDao.UpdateDealerGroup(dealerGroup);
            }
            catch (Exception ex)
            {
                LoggingFactory.GetLogger().Log(ex.ToString());
            }
        }

        public object GetAllDealerGroupsWs()
        {
            try
            {
                var peopleList = _dealerGroupDao.GetAllDealerGroups();
                return new { Result = "OK", Records = peopleList };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object CreateGroupWs(DealerGroup dealerGroup)
        {
            // validate inputs
            if (dealerGroup == null) return null;
            if (string.IsNullOrEmpty(dealerGroup.Code) || string.IsNullOrEmpty(dealerGroup.Name)) return null;

            try
            {
                var addedDealerGroup = _dealerGroupDao.AddDealerGroup(dealerGroup);
                return new { Result = "OK", Record = addedDealerGroup };
            }
            catch (Exception ex)
            {
                //throw;
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object UpdateGroupWs(DealerGroup dealerGroup)
        {
            if (dealerGroup == null) return null;
            try
            {
                _dealerGroupDao.UpdateDealerGroup(dealerGroup);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }

        public object DeleteGroupWs(int dealerGroupId)
        {
            try
            {
                _dealerGroupDao.DeleteDealerGroup(dealerGroupId);
                return new { Result = "OK" };
            }
            catch (Exception ex)
            {
                return new { Result = "ERROR", ex.Message };
            }
        }
        
        #endregion

        #region Helpers

        #endregion
    }
}
